<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>=^.^=</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="info">
	<p><a href="https://github.com/yomotsu/camera-controls">GitHub repo</a></p>
	<label><input type="checkbox" onchange="cameraControls.verticalDragToForward = this.checked">vertical drag to move forward</label>
	<br>
	<label><input type="checkbox" onchange="cameraControls.dollyToCursor = this.checked">dolly to cursor</label>
	<br>
	<button onclick="cameraControls.rotate(  45 * THREE.MathUtils.DEG2RAD, 0, true )">rotate theta 45deg</button>
	<button onclick="cameraControls.rotate( -90 * THREE.MathUtils.DEG2RAD, 0, true )">rotate theta -90deg</button>
	<button onclick="cameraControls.rotate( 360 * THREE.MathUtils.DEG2RAD, 0, true )">rotate theta 360deg</button>
	<button onclick="cameraControls.rotate( 0, 20 * THREE.MathUtils.DEG2RAD, true )">rotate phi 20deg</button>
	<br>
	<button onclick="cameraControls.normalizeRotations().reset( true )">reset</button>
</div>

<script async src="https://unpkg.com/es-module-shims@2.6.1/dist/es-module-shims.js"></script>
<script type="importmap">
{
	"imports": {
		"three": "https://unpkg.com/three@0.178.0/build/three.module.js",
		"three/addons/": "https://unpkg.com/three@0.178.0/examples/jsm/"
	}
}
</script>
<script type="module">
import * as THREE from 'three';
import { Octree } from 'three/addons/math/Octree.js';
import { OctreeHelper } from 'three/addons/helpers/OctreeHelper.js';
import CameraControls from './dist/camera-controls.module.js';
CameraControls.install( { THREE: THREE } );

const _ORIGIN = new THREE.Vector3( 0, 0, 0 );
const _v3A = new THREE.Vector3();
const _v3B = new THREE.Vector3();
const _v3C = new THREE.Vector3();
const _ray = new THREE.Ray();
const _rotationMatrix = new THREE.Matrix4();

class CustomCameraControls extends CameraControls {

	constructor( camera, domElement ) {

		super( camera, domElement );
		this.octree = new Octree();

	}

	_collisionTest() {

		let distance = Infinity;

		if ( ! this.octree ) return distance;

		const direction = _v3A.setFromSpherical( this._spherical ).divideScalar( this._spherical.radius );
		_rotationMatrix.lookAt( _ORIGIN, direction, this._camera.up );

		for ( let i = 0; i < 4; i ++ ) {

			const nearPlaneCorner = _v3B.copy( this._nearPlaneCorners[ i ] );
			nearPlaneCorner.applyMatrix4( _rotationMatrix );

			const origin = _v3C.addVectors( this._target, nearPlaneCorner );
			_ray.set( origin, direction );

			const intersect = this.octree.rayIntersect( _ray );

			if ( intersect && intersect.distance < distance ) {

				distance = intersect.distance;

			}

		}

		return distance;

	}

}

const width = window.innerWidth;
const height = window.innerHeight;
const clock = new THREE.Clock();
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 100 );
camera.position.set( 0, 0, 5 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
document.body.appendChild( renderer.domElement );

const cameraControls = new CustomCameraControls( camera, renderer.domElement );

const mesh = new THREE.Mesh(
	new THREE.BoxGeometry( 1, 1, 1 ),
	new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } )
);
scene.add( mesh );

const gridHelper = new THREE.GridHelper( 50, 50 );
gridHelper.position.y = - 1;
scene.add( gridHelper );

// colliders
const colliderMesh0 = new THREE.Mesh(
	new THREE.BoxGeometry( 3, 0.1, 3 ),
	new THREE.MeshNormalMaterial(),
);
colliderMesh0.position.y = - 1;
scene.add( colliderMesh0 );

const colliderMesh1 = new THREE.Mesh(
	new THREE.BoxGeometry( 1, 1, 1 ),
	new THREE.MeshNormalMaterial(),
);
colliderMesh1.position.set( 3, 0, 0 );
scene.add( colliderMesh1 );

const colliderMesh2 = new THREE.Mesh(
	new THREE.SphereGeometry( 1, 32, 32 ),
	new THREE.MeshNormalMaterial(),
);
colliderMesh2.position.set( - 3, 0, 0 );
scene.add( colliderMesh2 );

const colliderMesh3 = new THREE.Mesh(
	new THREE.TorusGeometry( 1, 0.3, 16, 64 ),
	new THREE.MeshNormalMaterial(),
);
colliderMesh3.position.set( 0, 0, - 3 );
scene.add( colliderMesh3 );

// make collider octree for the camera
cameraControls.octree.fromGraphNode( scene );

const helper = new OctreeHelper( cameraControls.octree );
scene.add( helper );


renderer.render( scene, camera );

( function anim () {

	const delta = clock.getDelta();
	const elapsed = clock.getElapsedTime();
	const updated = cameraControls.update( delta );

	// if ( elapsed > 130 ) { return; }

	requestAnimationFrame( anim );

	if ( updated ) {

		renderer.render( scene, camera );
		console.log( 'rendered' );

	}

} )();

// make variable available to browser console
globalThis.THREE = THREE;
globalThis.cameraControls = cameraControls;
</script>

</body>
</html>
